package com.app.util.common;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName: PrintExcel
 * @Description:发文和收文登记簿的打印工具类
 * @author Zhi.Hong
 * @company www.suncn.com.cn
 * @date 2017年8月7日 上午10:01:27
 */
public class PrintUtil {
	
	
	public void  printWord(String strData,String path,String fileName) throws Exception{
		//Blank Document  
        XWPFDocument document= new XWPFDocument();
        String sendDocName=path+"/"+fileName;
        FileUtil.createDir(path);
        //Write the Document in file system  
        FileOutputStream out = new FileOutputStream(new File(sendDocName));  // 下载路径/文件名称
  
        JSONObject dataJson = JSON.parseObject(strData);
        String strTitle = dataJson.getString("strTitle");
        String strPublisher = dataJson.getString("strPublisher");
        String strPubDate = dataJson.getString("strPubDate");
        String strContent = dataJson.getString("strContent");
        
        //添加标题  
        XWPFParagraph titleParagraph = document.createParagraph();  
        //设置段落居中  
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);  
  
        XWPFRun titleParagraphRun = titleParagraph.createRun();  
        titleParagraphRun.setText(strTitle);
        titleParagraphRun.setBold(true);
        titleParagraphRun.setFontFamily("simsun");
        titleParagraphRun.setFontSize(24);  
  
        //换行  
        XWPFParagraph paragraph1 = document.createParagraph();  
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraph1.setAlignment(ParagraphAlignment.CENTER);
        paragraphRun1.setText("发布人："+strPublisher+"    发布时间："+strPubDate+"");
        paragraphRun1.setFontFamily("simsun");
        
        
        String[] contents = strContent.split("</p>");
        for(String str:contents){
        	XWPFParagraph paragraphtemp = document.createParagraph();
            XWPFRun paragraphRuntemp = paragraphtemp.createRun();
//            paragraphRuntemp.
            
//            List<String> listStr = RegUtil.getSubUtil(str, "(?i)src=\"(.*?)\"");
            
            str=str.replaceAll("<[.[^<]]*>","");
            str=str.replaceAll("&ldquo;", "“");
            str=str.replaceAll("&rdquo;", "”");
            str=str.replaceAll("&quot;", "＂");
            str=str.replaceAll("&mdash;", "—");
            str=str.replaceAll("&nbsp;", " ");
//            str="<![if !vml]><img width=800 height=600 src='bfd17af55fc24c03a25e7547037fa3dc.files/image003.jpg' alt='http://www.ahghtz.com/_mediafile/newadmin/2018/06/08/2tllkrgzky.jpg' v:shapes='图片_x0020_23'><![endif]>";
            
            
        	paragraphRuntemp.setText(str); 
//    	 	
        	
        	
//            paragraphRuntemp.setColor("000000");  
            paragraphRuntemp.setFontFamily("仿宋");
            paragraphRuntemp.setFontSize(15);
            
            
//            CustomXWPFDocument customXWPFDocument = new CustomXWPFDocument();
//            FileInputStream in = new FileInputStream("D://Desktop/333333333333.jpg");
//    		byte[] ba = new byte[in.available()];
//    		in.read(ba);
//    		ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba);
//    		XWPFParagraph picture = customXWPFDocument.createParagraph();
//    		//添加图片
//    		customXWPFDocument.addPictureData(byteInputStream, CustomXWPFDocument.PICTURE_TYPE_JPEG);
//    		//图片大小、位置
//    		customXWPFDocument.createPicture(document.getAllPictures().size() - 1, 100, 100, picture);
    		
        }
        
        document.write(out);  
        out.close();  
  

	}
	
	
	/**
     * 导出Excel
     * @param sheetName sheet名称
     * @param title 标题
     * @param values 内容
     * @param wb HSSFWorkbook对象
     * @return
     */
    public void printExcel(String path,String fileName,String title,String strData) throws Exception{
    		String pathName=path+"/"+fileName;
    		FileUtil.createDir(path);
        
			FileOutputStream out = new FileOutputStream(new File(pathName));
  
	        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        	HSSFWorkbook wb = new HSSFWorkbook();
	
        	
	        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
	        HSSFSheet sheet = wb.createSheet("333");
	        
	        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
	        HSSFRow row = sheet.createRow(0);
	
	        HSSFCellStyle style = wb.createCellStyle();
	        style.setAlignment(HorizontalAlignment.CENTER);
	        HSSFFont titleFont = wb.createFont();  
	        titleFont.setFontName("微软雅黑");  
	        titleFont.setBold(true);
	        titleFont.setFontHeightInPoints((short) 12);  
	        style.setFont(titleFont);
	        
	        HSSFCellStyle contentStyle = wb.createCellStyle();
	        HSSFFont contentFont = wb.createFont();  
	        contentFont.setFontName("微软雅黑");  
	        contentStyle.setFont(contentFont);
	        contentStyle.setWrapText(true);
	        
	        
	        //声明列对象
	        HSSFCell cell = null;
	
        	String[] titles = title.split(",");
			
			JSONObject dataJson = JSON.parseObject(strData);
			JSONArray dataArray = dataJson.getJSONArray("list");
			
			Map<Integer,Integer> maxWidth = new HashMap<Integer,Integer>();
			
	        //创建标题
	        for(int i=0;i<titles.length;i++){
	            cell = row.createCell(i);
	            cell.setCellValue(titles[i]);
	            cell.setCellStyle(style);
	            
	            int length = titles[i].toString().getBytes().length  * 256 ;
	            //这里把宽度最大限制到15000
	            if (length>15000){
	                length = 15000;
	            }
	            maxWidth.put(i,Math.max(length,maxWidth.get(i)==null?0:maxWidth.get(i)));
	        }
	
	        String value = "";
	        //创建内容
	        for(int i=0;i<dataArray.size();i++){
	            row = sheet.createRow(i + 1);
	            for(int j=0;j<titles.length;j++){
	            	JSONArray array = dataArray.getJSONArray(i);
	            	value = array.getString(j);
	                //将内容按顺序赋给对应的列对象
	            	cell = row.createCell(j);
	            	cell.setCellValue(value);
	            	cell.setCellStyle(contentStyle);
	            	
	            	int length = value==null?0:value.toString().getBytes().length  * 256 ;
		            //这里把宽度最大限制到15000
		            if (length>15000){
		                length = 15000;
		            }
		            try{
			            maxWidth.put(j,Math.max(length,maxWidth.get(j)==null?0:maxWidth.get(j)));
			            }catch(Exception e){
			            	e.printStackTrace();
			            }
	            }
	        }
	        
	        for (int i= 0; i<titles.length;i++){
	            //设置列宽
	           sheet.setColumnWidth(i,maxWidth.get(i)==null?10:maxWidth.get(i));
	       }
	        
	        wb.write(out);
	        out.close();  
    }
	
}
